package spring.demo.security.userService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import spring.demo.security.dao.ResourceRepository;
import spring.demo.security.dao.RoleRepository;

import java.util.Collection;
import java.util.Map;

/**
 * Created by td on 2017/10/11.
 */
public class CusotmInvocationSecurityMetadataSourceService implements FilterInvocationSecurityMetadataSource {

    @Autowired
    private ResourceRepository resourceRepository;

    @Autowired
    private RoleRepository roleRepository;

    private static Map<String, Collection<ConfigAttribute>> resourceMap = null;

    @Override
    public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
        return null;
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return false;
    }

    private void loadResourceDefine() {  /*<span style="color:#33cc00;"> //一定要加上<span style="font-family: Arial, Helvetica, sans-serif;">@PostConstruct注解</span></span>
    <span style="color:#33cc00;">// 在Web服务器启动时，提取系统中的所有权限。</span>  */
       // List<Map<String,Object>> list =roleRepository.findAll();
 /*       List<String> query = new ArrayList<String>();
        if(list!=null && list.size()>0) {
            for(Map<String,Object> sr :list){
                //String name = sr.get("name")
                Object value = sr.get("name");
                String name = String.valueOf(value);
                query.add(name);
            }
        }
      *//*  <span style="color:#33cc00;">*//**//*
         * 应当是资源为key， 权限为value。 资源通常为url， 权限就是那些以ROLE_为前缀的角色。 一个资源可以由多个权限来访问。
         * sparta
         *//**//*</span>*//*
        resourceMap = new HashMap<String, Collection<ConfigAttribute>>();

        for (String auth : query) {
            ConfigAttribute ca = new SecurityConfig(auth);
            //List<Map<String,Object>> query1 = sResourceVODao.findByRoleName(auth);
            List<String> query1 = new ArrayList<String>();
            List<Map<String, Object>>  list1 = sResourceVODao.findByRoleName(auth);
            if(list1!=null && list1.size()>0) {
                for(Map<String, Object> map :list1){
                    Object value = map.get("resource_string");
                    String url = String.valueOf(value);
                    query1.add(url);
                }
            }
            for (String res : query1) {
                String url = res;

            *//*    <span style="color:#33cc00;">*//**//*
                 * 判断资源文件和权限的对应关系，如果已经存在相关的资源url，则要通过该url为key提取出权限集合，将权限增加到权限集合中。
                 * sparta
                 *//**//*</span>*//*
                if (resourceMap.containsKey(url)) {

                    Collection<ConfigAttribute> value = resourceMap.get(url);
                    value.add(ca);
                    resourceMap.put(url, value);
                } else {
                    Collection<ConfigAttribute> atts = new ArrayList<ConfigAttribute>();
                    atts.add(ca);
                    resourceMap.put(url, atts);
                }

            }
        }*/

    }
}
